/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui.admin.course;

import hibernate.dao.StaffDAO;
import hibernate.entity.Courses;
import hibernate.entity.Staffs;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.servlet.http.HttpSession;
import ui.admin.AbstractAdminBean;
import ui.common.AbstractUIBean;
import util.Converter;

/**
 *
 * @author Administrator
 */
@ManagedBean
@ViewScoped
public class CourseStaffManager extends AbstractAdminBean {

    List<Staffs> sourseStaff, targetStaff;
    StaffDAO staffDao = new StaffDAO();
    Courses currCourse;

    public List<Staffs> getSourseStaff() {
        return sourseStaff;
    }

    public void setSourseStaff(List<Staffs> sourseStaff) {
        this.sourseStaff = sourseStaff;
    }

    public List<Staffs> getTargetStaff() {
        return targetStaff;
    }

    public void setTargetStaff(List<Staffs> targetStaff) {
        this.targetStaff = targetStaff;
    }

    void loadData() {
        HttpSession session = getHttpSession();
        currCourse = (Courses) session.getAttribute("pickCourse");
        targetStaff = Converter.iteratorToList(currCourse.getStaffses().iterator());
        sourseStaff = staffDao.outOfCourse(currCourse);

    }

    public Courses getCurrCourse() {
        return currCourse;
    }

    public void setCurrCourse(Courses currCourse) {
        this.currCourse = currCourse;
    }

    public CourseStaffManager() {
        isAuthor();
        loadData();
    }

    /**
     * @param Staffs a staff for setting its information into his profile
     * @return void
     */
    public void add(Staffs s) {
        HttpSession session = getHttpSession();
        currCourse = (Courses) getHttpSession().getAttribute("pickCourse");
        List<Staffs> listS = (List<Staffs>) getHttpSession().getAttribute("listS");
        sourseStaff.remove(s);
        targetStaff.add(s);
        if (listS == null) {
            listS = new ArrayList<Staffs>();
        }

//        if (!isStaffExistInCourse(s, currCourse)) {
        listS.add(s);
//        }
        session.setAttribute("listS", listS);
    }

    /**
     * @param Staffs a staff to remove from the list
     * @return void
     */
    public void remove(Staffs s) {
        List<Staffs> listR = (List<Staffs>) getHttpSession().getAttribute("listR");
        if (listR == null) {
            listR = new ArrayList<Staffs>();
        }
        listR.add(s);
        targetStaff.remove(s);
        sourseStaff.add(s);
        getHttpSession().setAttribute("listR", listR);

    }

    /**
     * @return String
     * @see pickstaff page
     */
    public String update() {
        try {
            HttpSession session = getHttpSession();
            hbTransaction = hbSession.beginTransaction();
            List<Staffs> listR = (List<Staffs>) getHttpSession().getAttribute("listR");
            currCourse = (Courses) getHttpSession().getAttribute("pickCourse");
            if (listR != null) {
                for (Staffs s : listR) {
                    currCourse.getStaffses().remove(s);
                    currCourse = (Courses) hbSession.merge(currCourse);
                    hbSession.update(currCourse);
                    s = (Staffs) hbSession.merge(s);
                    hbSession.update(s);

                }
            }
            List<Staffs> listS = (List<Staffs>) getHttpSession().getAttribute("listS");
            if (listS == null) {
                listS = new ArrayList<Staffs>();
            }
            Courses c = (Courses) session.getAttribute("pickCourse");
            for (Staffs s : listS) {

                c.getStaffses().add(s);
                c = (Courses) hbSession.merge(c);
                hbSession.update(c);
                s = (Staffs) hbSession.merge(s);
                hbSession.update(s);
            }

            hbTransaction.commit();
            loadData();
            return "pickstaff";
        } catch (Exception e) {
            hbTransaction.rollback();
            return null;
        }
    }

    /**
     * @param Staffs a staff for checking whether it exists or not
     * @param Courses a course for checking staff's existence
     * @return boolean
          */
    private boolean isStaffExistInCourse(Staffs s, Courses c) {
        List<Staffs> listS = Converter.iteratorToList(c.getStaffses().iterator());
        boolean e = false;
        for (Staffs Staffs : listS) {
            if (s.getStaffId() == Staffs.getStaffId()) {
                e = true;
                break;
            }
        }
        return e;
    }
}
